package dao.impl;

import dao.BaseDao;
import dao.BaseDao02;
import dao.BookDao;
import pojo.Book;

import java.util.List;

public class BookDaoImpl extends BaseDao02 implements BookDao {//BaseDao02中已经获取JDBC连接
    @Override
    public int addBook(Book book) {
        String sql = "INSERT INTO t_book(`name`,`author`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?);";
        return update(sql,
                book.getName(),
                book.getAuthor(),
                book.getPrice(),
                book.getSales(),
                book.getStock(),
                book.getImgPath());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql = "delete from t_book where id = ?;";
        return update(sql,id);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set `name`=?,`author`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=?where id = ?;";
        return update(sql,
                book.getName(),
                book.getAuthor(),
                book.getPrice(),
                book.getSales(),
                book.getStock(),
                book.getImgPath(),
                book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql = "SELECT id,`name`,author,price,sales,stock,img_path imgPath FROM t_book where id = ?;";
        return queryForOne(sql,Book.class,id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql = "SELECT id,`name`,author,price,sales,stock,img_path imgPath FROM t_book;";
        return queryForMore(sql,Book.class);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from t_book;";
        Number count = (Number) queryForSpecial(sql);
        return count.intValue();
    }

    @Override
    public List<Book> queryForItems(int begin, Integer pageSize) {
        String sql = "SELECT id,`name`,author,price,sales,stock,img_path imgPath FROM t_book limit ?,?;";
        return queryForMore(sql,Book.class,begin,pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min,int max) {
        String sql = "select count(*) from t_book where price between ? and ?;";
        Number count = (Number) queryForSpecial(sql,min,max);
        return count.intValue();
    }

    @Override
    public List<Book> queryForItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql = "SELECT id,`name`,author,price,sales,stock,img_path imgPath FROM t_book where price between ? and ? order by price asc limit ?,?;";
        return queryForMore(sql,Book.class,min,max,begin,pageSize);
    }
}
